﻿Imports System.IO
Imports System.Data
Imports amod2
Public Class DlgDBGenerator
    Dim script As String
    Dim main As Main
    ' referencia cize sa globalne zmeni
    ' Dim SQLSystemScript As String = ""      ' skript pre vytvorenie Systemovych tabuliek
    ' Dim systemSQLscriptLocation As String
    ' Dim DBLocation As String
    ' Dim settingsLocation As String

    ''' <summary>
    ''' Prenesie referenciu na objekt aDat, prerobit na konstruktor
    ''' </summary>
    ''' <param name="main"></param>
    ''' <remarks></remarks>
    Public Sub giveData(ByRef main As Main)
        Me.main = main
    End Sub


    ''' <summary>
    ''' Vyberiem cestu pre Systemovy Skript pre vytvorenie Systemovych tabuliek
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSystemSQLScripts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSystemSQLScripts.Click
        Dim OFD As OpenFileDialog = New OpenFileDialog()
        OFD.Filter = "sql(*.sql)|*.sql"
        OFD.ShowDialog()
        Dim fileName As String = OFD.FileName
        If (OFD.FileName.Length > 0) Then


            Dim str As Stream = OFD.OpenFile()
            Dim strReader As StreamReader = New StreamReader(str)
            script = strReader.ReadToEnd()  ' nacitam si do atributu cely skript
            Me.txtBx_systemScripts.Text = OFD.FileName
        End If
    End Sub


    Private Sub DlgDBGenerator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.initComponents() 'inicializacia komponentov
    End Sub

    ''' <summary>
    ''' nastavim si verzie 'ale ked nemam ziadny importovany xMod tak mam verziu 1 - neviem preco otazka
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub initComponents()
        cmbBx_version.Items.Add("Latest")
        cmbBx_version.SelectedIndex = 0
        'For Each ver As Int32 In aDat.GetAllVersions()

        '    cmbBx_version.Items.Add(ver.ToString())
        'Next
    End Sub

    ''' <summary>
    ''' Save Settings of Database Generator
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btn_saveSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_saveSettings.Click
        
        If (Not Me.checkSettingsForSaving(txt_dbFileName.Text, txtBxServer.Text, txtBxDBName.Text, cmbBx_version.SelectedItem.ToString(), txtBx_DBSaveLocation.Text)) Then
            MessageBox.Show("CHYBA - Nie su nastavené všetky požadované položky ", "Generovanie Databázy")
        Else
            Dim fsd As New SaveFileDialog()
            fsd.Filter = "xml(*.xml)|*.xml"
            fsd.ShowDialog()
            If (fsd.FileName.Length > 0) Then


                Dim ver As Integer
                ' settingsLocation = fsd.FileName
                If (cmbBx_version.SelectedItem.ToString() = "Latest") Then 'ak je zaskrtnuty checkBox Last Version
                    ver = -1 'tak last version
                Else
                    ver = Convert.ToInt32(cmbBx_version.SelectedItem.ToString())
                End If
                ' treba spravit este kontrolu na vsetky zadane hodnoty dorobit

                Dim dbGenSett As New DBGenSettings(txt_dbFileName.Text, txtBxServer.Text, txtBxDBName.Text, ver, txtBx_systemScripts.Text, txtBx_DBSaveLocation.Text, "", txt_DBscriptSaveFile.Text)
                dbGenSett.SavePath = fsd.FileName
                Try
                    XML_worker.createXmlSettingsFile(dbGenSett)
                Catch ee As Exception
                    MessageBox.Show("Chyba - nepodarilo sa ulozit Setting File pre DB generator" + ee.Message)
                End Try
                Me.main.DBGeneratorSettings = dbGenSett
                Me.SetComponents(dbGenSett)
            End If
        End If
    End Sub
    ''' <summary>
    ''' Skontroluje ci su zadane vsetky potrebne polozky
    ''' </summary>
    ''' <param name="server"></param>
    ''' <param name="dbName"></param>
    ''' <param name="version"></param>
    ''' <param name="dbPath"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function checkSettingsForSaving(ByVal dbFileName As String, ByVal server As String, ByVal dbName As String, ByVal version As String, ByVal dbPath As String) As Boolean
        If ((server.Length = 0) Or (dbName.Length = 0) Or (dbPath.Length = 0) Or (dbFileName.Length = 0)) Then
            Return False
        End If
        Return True
    End Function

    ''' <summary>
    ''' Nastavim si cestu kam chcem ulozit vytvorene databazky
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btn_selectDBlocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_selectDBlocation.Click
        Dim fbd As New FolderBrowserDialog
        fbd.ShowDialog()
        txtBx_DBSaveLocation.Text = fbd.SelectedPath()
    End Sub



    Private Sub btnLoadSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadSettings.Click
        Dim ofd As OpenFileDialog = New OpenFileDialog()
        ofd.Filter = "xml(*.xml)|*.xml"
        ofd.FileName = "E:\data projekt kros\db_settings.xml"
        ofd.ShowDialog()
        Dim path As String = ofd.FileName
        If (Me.checkPath(path)) Then
            Try
                Me.main.DBGeneratorSettings = XML_worker.openDBGenSettings(path)
                '   settingsLocation = path
                Me.SetComponents(Me.main.DBGeneratorSettings)
            Catch ee As Exception
                MessageBox.Show("CHYBA - Problem s načítaním nastavenia DB Generatora. Pravdepodobne chybny ""Settings subor""", "Generovanie Databázy")
            End Try


        End If
    End Sub
    ''' <summary>
    ''' Skontroluje ci je zadana cesta 
    ''' </summary>
    ''' <param name="path"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function checkPath(ByVal path As String) As Boolean
        If (path.Length = 0) Then
            Return False
        End If
        Return True
    End Function

    ' asi zbytocne aby som si z vonku nastavoval komponenty ked to mzoem spravit z vnutra,
    ' po vytvoreni formularu kde prenesiem referenciou aDat
    ''' <summary>
    ''' nastavi komponenty obsahom ktory je v nastavovacom objekte pre DB Generator
    ''' </summary>
    ''' <param name="dbGenSett">objekt nastavenia DB Generatora</param>
    ''' <remarks></remarks>
    Public Sub SetComponents(ByVal dbGenSett As DBGenSettings)
        txt_dbFileName.Text = dbGenSett.DBFileName
        txtBx_DBSaveLocation.Text = dbGenSett.DbPath
        txtBx_systemScripts.Text = dbGenSett.SystSQLscriptsPath
        txtBxDBName.Text = dbGenSett.DbName
        txtBxServer.Text = dbGenSett.ServerName

        If (dbGenSett.Ver = -1) Then    ' -1 reprezentuje POSLEDNU VERZIU!
            cmbBx_version.SelectedItem = "Latest"
        Else
            cmbBx_version.SelectedText = dbGenSett.Ver
        End If
        txt_DBscriptSaveFile.Text = dbGenSett.SavePath
    End Sub

    ''' <summary>
    ''' Vytvorim si instanciu nastaveni pri stlaceni buttonu OK
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks> 
    Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click
        If (Not Me.checkSettingsForSaving(txt_dbFileName.Text, txtBxServer.Text, txtBxDBName.Text, cmbBx_version.SelectedItem.ToString(), txtBx_DBSaveLocation.Text)) Then
            MessageBox.Show("CHYBA - Nie su nastavené všetky požadované položky ", "Generovanie Databázy")
        Else
            Dim ver As Integer
            If (cmbBx_version.SelectedItem.ToString() = "Latest") Then
                ver = -1
            Else
                ver = cmbBx_version.SelectedValue.ToString()
            End If
            'vytvorenie instancie DB Generatora Nastavenia Dim dbGenSett As New DBGenSettings
            Me.main.DBGeneratorSettings = New DBGenSettings(txt_dbFileName.Text, txtBxServer.Text, txtBxDBName.Text, ver, txtBx_systemScripts.Text, txtBx_DBSaveLocation.Text, Me.script, txt_DBscriptSaveFile.Text)
            Me.Close()
        End If
    End Sub

    ''' <summary>
    ''' Zavre okno bez ulozenia zmien
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cancel.Click
        Me.Close()
    End Sub

    Private Sub txtBxDBName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBxDBName.TextChanged

    End Sub

    Private Sub ToolTip3_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs)

    End Sub



    Private Sub btn_saveScriptFilePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_saveScriptFilePath.Click
        Dim svDlg As New SaveFileDialog()
        svDlg.Filter = "sql(*.sql)|*.sql"
        svDlg.FileName = "db_scripts.sql"
        svDlg.ShowDialog()

        Dim path As String = svDlg.FileName
        txt_DBscriptSaveFile.Text = path
    End Sub
End Class